import string
import copy
import scipy
import Tkinter, tkFileDialog
import numpy as np
import pandas as pd
from scipy.spatial.distance import cdist
import matplotlib.pyplot as plt
import os
import sys
import datetime
import matplotlib.units
import re
from numba import jit,int32
import time
from PIL import Image
import pims
import glob
sys.path.append(os.path.abspath("C:\Users\Scherer Lab E\Documents\GitHub\Python_Data_Analysis"))
import common_functions
'''Import the Matlab Gui Data'''
data_dir="C:\Users\Scherer Lab E\Downloads\TrackingGUI_and_associated_files_20July2014 My Version"
os.chdir(data_dir)
file_list_proc = glob.glob('Mov_012014*pre-linking_processed.mat')
data_list_proc = [common_functions.import_matlab_gui(i) for i in file_list_proc]
data_list_proc = [common_functions.matlab_gui_to_data_frame(i) for i in data_list_proc]
os.chdir("J:\Pat's Projects\Dynamical Phase Transition\Exp01201401\Half over Nanoplate")
folder_list = glob.glob("Mov_012*L=?\\")
'''Do y-flip of position data and add polar coordinates'''
transformed_data_list_proc = []
for df in data_list_proc:
new_flip = common_functions.y_axis_flip(df, 390)
temp_fit_params = common_functions.least_sq_fit_circle(new_flip)
common_functions.polar_coor_data_frame(new_flip, temp_fit_params[0], temp_fit_params[1])
transformed_data_list_proc.append(new_flip)
def find_frames_with_no_particles_in_theta_region(df, theta_limits, buffer_time=1):
"""Finds frames where there are no particles in a defined theta region for
time +/- the buffer time
:params df: The DataFrame with all the particle trajectories.
:params theta_limits: List of length 2 that defines the lower and upper
bounds of theta that there could be no particles.
:params buffer_time: the number of frames before and after finding a frame
with no particles must there also be no particles to be counted."""
df_theta = df.query('@theta_limits[0] < theta < @theta_limits[1]')
unique_frames = df_theta.drop_duplicates(subset='frame')
low_frames = unique_frames[unique_frames.frame.shift(-1) - unique_frames.frame > 1+2*buffer_time]
high_frames = unique_frames[unique_frames.frame - unique_frames.frame.shift(1) > 1+2*buffer_time]
zipped_frames = zip(low_frames.frame, high_frames.frame)
frame_list = []
for low, high in zipped_frames:
frame_list += range(int(low)+1+buffer_time, int(high)-buffer_time)
return frame_list
def background_subtraction_and_save_frame_subset(df, theta_limits, buffer_time, folder):
"""Used with "find_frames_with_no_particles_in_theta_region" to find a
background image using only specific frames. This background is subtracted
from each raw data image and saved in a new folder.
:params df: The DataFrame with all the particle trajectories.
:params theta_limits: List of length 2 that defines the lower and upper
bounds of theta that there could be no particles.
:params buffer_time: the number of frames before and after finding a frame
with no particles must there also be no particles to be counted.
:param folder: The path where the raw data is. Expects a tif image sequence
that is in order when glob is called"""
image_seq = pims.ImageSequence(folder+'*.tif')
frames = find_frames_with_no_particles_in_theta_region(df, theta_limits, buffer_time)
median = np.median(image_seq[frames], axis=0)
global_min = abs(np.min(image_seq - median))
try:
os.mkdir(folder[:-1]+' Processed_Cherry_Picked')
except:
pass
for num, img in enumerate(image_seq):
new_img = img-median
new_img = global_min + new_img
pil_img = Image.fromarray(new_img)
pil_img.save(folder[:-1]+' Processed_Cherry_Picked\\'+str(10000+num)+".tif")
return median
for num, folder in enumerate(folder_list):
print folder
median = background_subtraction_and_save_frame_subset(transformed_data_list_proc[num], theta_limits=[240,300], buffer_time=1, folder=folder)
plt.imshow(median, cmap='gray')
plt.show()
median = background_subtraction_and_save_frame_subset(transformed_data_list_proc[-1], theta_limits=[250,300], buffer_time=1, folder=glob.glob("Mov_012*L=??\\")[0])
plt.imshow(median, cmap='gray')
plt.show()